<?php

use yii\helpers\Html;
use yii\grid\GridView;

use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\CategorySearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '分类列表';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="category-index">
    
    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

       <p><?= 
    Html::a('创建栏目', ['create'], [
        'class' => 'btn btn-success',
        'id' => 'create', // 按钮的id随意
        'data-toggle' => 'modal', // 固定写法
        'data-target' => '#operate-modal', // 等于modal begin中设定的参数id值
    ]) 
?></p>
       

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        //'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            'id',
            'name',
            'title',
//            'pid',
            'sort',
//            'list_row',
             [
                 'attribute' => 'status',
                  'value' =>function($model){
                        $result = "";
                        switch ($model->status){
                            case "1":$result="正常";break;
                            case "-1":$result = "禁用";break;
                            default :$result="正常";
                        }
                        
                        return $result;
                  },
             ],
//            ['label'=>'分类名称','value' => 'title'],
//            ['label'=>'父类ID','value' => 'pid'],
//            ['label'=>'排序','value' => 'sort'],
//            ['label'=>'显示数目','value' => 'list_row'],
//            ['label'=>'状态','value' => 'status'],
            //'list_row',
            //'meta_title',
            //'keywords',
            //'description',
            //'template_index',
            //'template_lists',
            //'template_detail',
            //'template_edit',
            //'model',
            //'model_sub',
            //'type',
            //'link_id',
            //'allow_publish',
            //'display',
            //'reply',
            //'check',
            //'reply_model',
            //'extend:ntext',
            //'create_time',
            //'update_time',
            //'status',
            //'icon',
            //'groups',
            
            ['class' => 'yii\grid\ActionColumn',
                    'template' => '{update}  {delete}',
                    'header' => '操作',
                    'buttons' => [
                        'update' => function ($url, $model, $key) {
                            return Html::a('<i class="glyphicon glyphicon-eye-open"></i>', "javascript:void(0)", [
                                    'title' => '栏目信息', 
                                    // btn-update 目标class
                                    'class' => 'btn-update',
                                    // 固定写法
                                    'data-toggle' => 'modal',
                                    // 指向modal中begin设定的id
                                    'data-target' => '#operate-modal',
                            ]); 
        
                         
                        },
                        'delete' => function ($url, $model, $key) {
                            return Html::a('<i class="glyphicon glyphicon-trash"></i>', $url, [
                                'title' => '删除',
                                'class' => '',
                                'data-method' =>'post',
                             
                            ]); 
                        },
                    ],
                ],
        ],
    ]); ?>
</div>






<?php



// 异步请求的地址
$requestCreateUrl = Url::toRoute('create');
$js = <<<JS
// 创建操作
$('#create').on('click', function () {
    $('.modal-title').html('创建栏目');
    $.get('{$requestCreateUrl}',
        function (data) {    
            // 弹窗的主题渲染页
            $('.modal-body').html(data);
        }  
    );
});
JS;
$this->registerJs($js);




// 更新
$requestUpdateUrl = Url::toRoute('update');
$js = <<<JS
    // 更新操作
    $('.btn-update').on('click', function () {
        $('.modal-title').html('栏目信息');
        //$.get('{$requestUpdateUrl}', { id: $(this).closest('tr').data('key') },
  
        $.get('{$requestUpdateUrl}', { id: $(this).parents("tr").children("td:eq(1)").html()},
            function (data) {
                $('.modal-body').html(data);
            }  
        );
    });
JS;
$this->registerJs($js);
?>



<?php
    use yii\bootstrap\Modal;
    Modal::begin([
        'id' => 'operate-modal',
        'header' => '<h4 class="modal-title"></h4>',
    ]); 
    Modal::end();
?>
